package ar.edu.sovietrussia.customcomponents.searchbook;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import services.book.BookService;
import ar.edu.sovietrussia.customcomponents.bookviews.BookModel;
import dtos.BookInfoDTO;

/**
 */
public class SearchBookPanel extends Panel {

    @SpringBean
    private BookService bookService;

    private List<BookInfoDTO> lastSearchResult;

    private static final long serialVersionUID = 1L;

    private MainSearchComponent mainSearch;

    public void setMainSearch(final MainSearchComponent mainSearch) {
        this.mainSearch = mainSearch;
    }

    public MainSearchComponent getMainSearch() {
        return this.mainSearch;
    }

    public void setParent(final MainSearchComponent search) {
        this.mainSearch = search;
    }

    public SearchBookPanel(final String id, final SearchBookPanelModel model,
            final MainSearchComponent mainSearchComponent) {
        super(id, model);
        this.mainSearch = mainSearchComponent;
        this.setLastSearchResult(new ArrayList<BookInfoDTO>());
        this.addComponents();
    }

    private void addComponents() {
        this.addSearchForm("searchForm", this);

    }

    private void addSearchForm(final String id, final MarkupContainer container) {

        final BookModel bookModel = this.getModel().getBookModel();
        Label formLabel = new Label("formLabel", "Search book title:");
        Form<BookModel> bookForm = new Form(id, bookModel);

        AjaxButton submitButton = new AjaxButton("submit") {
            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
                BookInfoDTO example = bookModel.getObject();
                SearchBookPanel.this.onSearchSubmit(example, target);
            }

            @Override
            protected void onError(final AjaxRequestTarget target, final Form<?> form) {
                target.add(SearchBookPanel.this.getFeedbackTarget());
            }
        };
        bookForm.add(formLabel);
        bookForm.add(new RequiredTextField<String>("title"));
        bookForm.setDefaultButton(submitButton);
        bookForm.add(submitButton);
        container.add(bookForm);
    }

    private SearchBookPanelModel getModel() {
        return (SearchBookPanelModel) this.getDefaultModel();
    }

    public void onSearchSubmit(final BookInfoDTO example, final AjaxRequestTarget target) {
        this.setLastSearchResult(this.getBookService().searchByTitle(example.getTitle()));
        this.searchSubmited(target);
    }

    protected FeedbackPanel getFeedbackTarget() {
        // redefinir
        return null;
    }

    public void searchSubmited(final AjaxRequestTarget target) {
        // redefinir de arriba
    }

    public void setBookService(final BookService bookService) {
        this.bookService = bookService;
    }

    public BookService getBookService() {
        return this.bookService;
    }

    public void setLastSearchResult(final List<BookInfoDTO> lastSearchResult) {
        this.lastSearchResult = lastSearchResult;
    }

    public List<BookInfoDTO> getLastSearchResult() {
        return this.lastSearchResult;
    }
}
